import {
  getOrderDetail,
  wxPay,
  sendTemplateAfterPay
} from "../../../api.js"
Page({
  data: {
    this_num: 0,
    money_num: 0,
    batchcode: ''
  },
  onLoad: function (options) {
    if (options.batchcode) {
      this.init(options.batchcode)
      this.setData({
        batchcode: options.batchcode
      })
    }
    // 支付倒计时
    this.startCount()
  },
  init(code) {
    const xp = this
    let token = getApp().globalData.Store.token
    getOrderDetail(token, code).then(res => {
      if (res.data.code === 666) {
        xp.payWhenLess1W(res.data.data.order)
        var nums = 0
        for (var i = 0; i < res.data.data.unit_orders.length; i++) {
          if (res.data.data.unit_orders[i].payment_status == 0) {
            nums++
          }
        }
        this.setData({
          order: res.data.data,
          money_num: nums
        })
      } else {
        wx.showToast({
          title: res.data.msg,
          icon: "none"
        })
      }
    })
  },
  startCount(min = 5, sec = 0) {
    let timeout = setTimeout(() => {
      clearTimeout(timeout)
      if (sec === 0) {
        sec = 59
        min -= 1

      } else {
        sec -= 1
      }
      let _sec = sec < 10 ? `0${sec}` : sec
      let _min = min < 10 ? `0${min}` : min
      this.setData({
        countDown: `${_min}分${_sec}秒`
      })
      if (min >= 0 && sec > 0) {
        this.startCount(min, sec)
      }

    }, 1000)
  },
  /**
   * 检查小于1W 直接进行支付
   */
  payWhenLess1W(order) {
    if (order.total_price < 10000) {
      let total_fee = (order.total_price * 100 - order.coupon_price * 100) / 100 <= 0 ? 0.01 : (order.total_price * 100 - order.coupon_price * 100) / 100
      let store = getApp().globalData.Store
      let token = store.token
      let business_id = store.business.id
      let openid = store.user.openid
      let batchcode = order.batchcode
      let params = {
        token,
        openid,
        batchcode,
        total_fee,
        business_id,
      }

      this.invokeWxPay(params)
    }
  },
  /**
   * 支付按钮
   */
  payforOrder(e) {
    const xp = this
    let total_fee = e.target.dataset.total
    let store = getApp().globalData.Store
    let token = store.token
    let business_id = store.business.id
    let openid = store.user.openid
    let params = {
      token,
      openid,
      total_fee,
      business_id,
    }

    // 区别分笔支付
    if (e.target.dataset.code) {
      params.batchcode = e.target.dataset.code
    } else {
      params.batchcode = this.data.order.order.batchcode
    }
    xp.invokeWxPay(params)
  },
  /**
   * 调用微信支付
   */
  invokeWxPay(params) {
    var xp = this
    wxPay(params).then(res => {
      if (res.data) {
        var prepay_id = res.data.package.split("=")[1]
        wx.requestPayment({
          ...res.data,
          success(res) {
            wx.redirectTo({
              url: `/pages/money/billCompletion/billCompletion?order=${JSON.stringify(xp.data.order.order)}`,
            })
          }
        })
      }
    })
  },


  //分笔支付
  payforOrder_pay(e) {
    const xp = this
    let total_fee = e.target.dataset.total
    let index = e.target.dataset.index
    let store = getApp().globalData.Store
    let token = store.token
    let business_id = store.business.id
    let openid = store.user.openid
    let params = {
      token,
      openid,
      total_fee,
      business_id,
    }

    // 区别分笔支付
    if (e.target.dataset.code) {
      params.batchcode = e.target.dataset.code
    } else {
      params.batchcode = this.data.order.order.batchcode
    }
    xp.invokeWxPay_pay(params, index)
  },
  /**
   * 调用微信支付
   */
  invokeWxPay_pay(params, index) {
    var xp = this
    wxPay(params).then(res => {
      if (res.data) {
        wx.requestPayment({
          ...res.data,
          success(res) {
            xp.data.order.unit_orders[index].payment_status = 1
            xp.setData({
              money_num: xp.data.money_num - 1,
              order: xp.data.order
            })
            if (xp.data.money_num == 0) {
              wx.redirectTo({
                url: `/pages/money/billCompletion/billCompletion?order=${JSON.stringify(xp.data.order.order)}`,
              })
            }
          }
        })
      }
    })
  },
  onShow() {
    getApp().startpage(new Date().getTime())
  },
  onHide() {
    getApp().endpage(new Date().getTime())
  }
})
